package com.jarvis.pzz.modules.requirement;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jarvis.pzz.R;
import com.jarvis.pzz.common.BaseFragment;
import com.jarvis.pzz.common.ResponseData;
import com.jarvis.pzz.models.AdvertisesmetListModel;
import com.jarvis.pzz.models.RequireBannerModel;
import com.jarvis.pzz.modules.buyshop.BuyShopActivity;
import com.jarvis.pzz.modules.findshop.FindShopActivity;
import com.jarvis.pzz.modules.login.LoginActivity;
import com.jarvis.pzz.modules.sellshop.SellShopActivity;
import com.jarvis.pzz.seervers.RequestService;
import com.jarvis.pzz.util.SharePreferenceKey;
import com.jarvis.pzz.util.net.RequestApi;
import com.jarvis.pzz.widget.BannerHeaderView;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by jarvis.liu on 2016-11-02.
 */

public class RequirementFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.lin_title)
    RelativeLayout lin_title;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.imv_find_shop)
    ImageView imv_find_shop;
    @BindView(R.id.imv_to_shop)
    ImageView imv_to_shop;
    @BindView(R.id.imv_find_shop_new)
    ImageView imv_find_shop_new;
    @BindView(R.id.imv_to_shop_new)
    ImageView imv_to_shop_new;
    @BindView(R.id.imv_buy_shop)
    ImageView imv_buy_shop;
    @BindView(R.id.imv_sell_shop)
    ImageView imv_sell_shop;
    @BindView(R.id.banner)
    BannerHeaderView banner;

    private int status = 0;

    private List<AdvertisesmetListModel> advList = new ArrayList<>();
    private RequestService service;

    public static RequirementFragment newInstance() {
        Bundle args = new Bundle();
        RequirementFragment fragment = new RequirementFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_requirement;
    }
    private void loadDaTa(){
        showDialog("loading");
        Call<ResponseData<RequireBannerModel>> index = service.getShopInfoAdvList();
        index.enqueue(new Callback<ResponseData<RequireBannerModel>>() {
            @Override
            public void onResponse(Call<ResponseData<RequireBannerModel>> call, Response<ResponseData<RequireBannerModel>> response) {
                disMiss();
                if (response.body() != null) {
                    int resultCode = response.body().getResultCode();
                    if (resultCode == 200) {
                        advList = response.body().getData().getAdvertisesmetList();
                        banner.setImgUrlData(advList);
                    } else {
                        showTextToast(response.body().getMessage());
                    }
                } else {
                    showTextToast("请求失败");
                }
            }

            @Override
            public void onFailure(Call<ResponseData<RequireBannerModel>> call, Throwable t) {
                disMiss();
                showTextToast(RequestApi.detailError(t));
            }
        });
    }
    @Override
    public void initData() {
        loadDaTa();
    }

    @Override
    protected void initView() {
        service = RequestApi.create(RequestService.class);
        setListener();
        setBar();
    }

    /**
     * 设置监听事件
     */
    public void setListener() {
        imv_find_shop.setOnClickListener(this);
        imv_to_shop.setOnClickListener(this);
        imv_find_shop_new.setOnClickListener(this);
        imv_to_shop_new.setOnClickListener(this);
        imv_buy_shop.setOnClickListener(this);
        imv_sell_shop.setOnClickListener(this);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            status = 1;
            setStatusWhite(false);
            MobclickAgent.onPageStart(getClass().toString()); //统计页面，"MainScreen"为页面名称，可自定义
            MobclickAgent.onResume(getContext());
            loadDaTa();
        } else {
            status = 2;
            MobclickAgent.onPageEnd(getClass().toString());
            MobclickAgent.onPause(getContext());
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (status == 0) {
            MobclickAgent.onPageEnd(getClass().toString());
            MobclickAgent.onPause(getContext());
        }
    }
    private void setBar() {
        lin_title.setVisibility(View.VISIBLE);
        lin_title.getBackground().setAlpha(255);
        int statusBarHeight1 = -1;
        //获取status_bar_height资源的ID
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
        }
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(lin_title.getLayoutParams());
        lp.height=lp.height+statusBarHeight1;
        //lp.setMargins(0, statusBarHeight1, 0, 0);
        lin_title.setLayoutParams(lp);
        tv_title.setPadding(0, statusBarHeight1, 0, 0);
    }
    @Override
    public void onResume() {
        super.onResume();
        setStatusWhite(false);
        if (status == 0) {
            MobclickAgent.onPageStart(getClass().toString()); //统计页面，"MainScreen"为页面名称，可自定义
            MobclickAgent.onResume(getContext());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_title:
                break;
            case R.id.imv_find_shop:
            case R.id.imv_find_shop_new:
                //找铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 2;
                    startActivity(new Intent(getActivity(), FindShopActivity.class));
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.imv_to_shop:
            case R.id.imv_to_shop_new:
                //转铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 3;
                    Intent intent = new Intent(getActivity(), SellShopActivity.class);
                    intent.putExtra("TO_SELL", 0);
                    startActivity(intent);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.imv_buy_shop:
                //买铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 4;
                    startActivity(new Intent(getActivity(), BuyShopActivity.class));
                } else {
                    startActivity(LoginActivity.class);
                }
                break;
            case R.id.imv_sell_shop:
                //卖铺
                if (isLogin()) {
                    SharePreferenceKey.SHOP_STATE = 5;
                    Intent intent2 = new Intent(getActivity(), SellShopActivity.class);
                    intent2.putExtra("TO_SELL", 1);
                    startActivity(intent2);
                } else {
                    startActivity(LoginActivity.class);
                }
                break;

        }
    }
}
